package com.itheima.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.book.interceptor.BaseContext;
import com.itheima.book.pojo.dto.RegisterDTO;
import com.itheima.book.pojo.dto.UserLoginDto;
import com.itheima.book.pojo.entity.User;
import com.itheima.book.service.UserService;
import com.itheima.book.mapper.UserMapper;
import com.itheima.book.utils.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 86178
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-04-25 14:59:19
*/
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public User login(UserLoginDto userLoginDto) {
        //构造查询条件
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(User::getUsername,userLoginDto.getUsername())
                .eq(User::getPassword,userLoginDto.getPassword());

        User user = userMapper.selectOne(wrapper);

        if (user==null){
            throw new BaseException("登录失败!");
        }

        log.info("当前用户id:{}",user.getId());

        return user;
    }

    @Override
    public void getByphone(String mobile) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(User::getPhone).eq(User::getPhone,mobile);

        User user = userMapper.selectOne(wrapper);

        if (user==null){
            throw new BaseException("用户不存在");
        }
    }

    @Override
    public void getByemail(String email) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(User::getEmail).eq(User::getEmail,email);

        User user = userMapper.selectOne(wrapper);

        if (user==null){
            throw new BaseException("用户不存在");
        }
    }

    @Override
    public User loginsms(UserLoginDto userLoginDto) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone,userLoginDto.getPhone());
        //1.判断用户是否存在
        User user = userMapper.selectOne(wrapper);

        if (user==null){
            throw new BaseException("用户不存在");
        }


        try{
            //2.判断是否拿到验证码
            Object code = redisTemplate.opsForSet().members(userLoginDto.getPhone()+"code").iterator().next();
            log.info("拿到验证码:{}",code);

            //3.判断验证码是否正确
            if (!userLoginDto.getCode().equals(code)){
                throw new BaseException("无效验证码");
            }

        }catch (Exception e){
            throw new BaseException("无效验证码");
        }
        return user;
    }

    @Override
    public User loginemail(UserLoginDto userLoginDto) {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmail,userLoginDto.getEmail());
        //1.判断用户是否存在
        User user = userMapper.selectOne(wrapper);

        if (user==null){
            throw new BaseException("用户不存在");
        }


        try{
            //2.判断是否拿到验证码
            Object code = redisTemplate.opsForSet().members(userLoginDto.getEmail()+"code").iterator().next();
            log.info("拿到验证码:{}",code);

            //3.判断验证码是否正确
            if (!userLoginDto.getCode().equals(code)){
                throw new BaseException("无效验证码");
            }

        }catch (Exception e){
            throw new BaseException("无效验证码");
        }
        return user;
    }

    @Override
    public void getByphonesingup(String mobile) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone,mobile);
        //1.判断用户是否存在
        User user = userMapper.selectOne(wrapper);

        if (user!=null){
            throw new BaseException("用户已存在");
        }
    }

    @Override
    public void register(RegisterDTO registerDTO) {
        //1.判断用户是否已存
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,registerDTO.getUsername())
                .or()
                .eq(User::getPhone,registerDTO.getPhone());

        User user = userMapper.selectOne(wrapper);

        //2.存在则拒绝注册
        if (user!=null){
            throw new BaseException("用户已存在");
        }
        //3.判断验证码是否正确
        try{
            //2.判断是否拿到验证码
            Object code = redisTemplate.opsForSet().members(registerDTO.getPhone()+"code").iterator().next();
            log.info("拿到验证码:{}",code);

            //3.判断验证码是否正确
            if (!registerDTO.getCode().equals(code)){
                throw new BaseException("无效验证码");
            }

        }catch (Exception e){
            throw new BaseException("无效验证码");
        }
        //4.不存在则注册成功且验证码正确
        user = new User();
        user.setUsername(registerDTO.getUsername());
        user.setPassword(registerDTO.getPassword());
        user.setPhone(registerDTO.getPhone());
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setEmail("");
        user.setAvatar("");
        userMapper.insert(user);
    }
}




